package com.example.admin.yuejian21.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.admin.yuejian21.Adapter.CommentAdapter;
import com.example.admin.yuejian21.Adapter.RenqiAdapter;
import com.example.admin.yuejian21.Bean.Club;
import com.example.admin.yuejian21.Bean.Login;
import com.example.admin.yuejian21.Bean.Pic;
import com.example.admin.yuejian21.Bean.PicDetail;
import com.example.admin.yuejian21.Bean.comment;
import com.example.admin.yuejian21.Model.UserModel;
import com.example.admin.yuejian21.R;
import com.example.admin.yuejian21.Service.Service;

import org.w3c.dom.Comment;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.admin.yuejian21.Adapter.PicAdapter.photo1;
import static com.example.admin.yuejian21.Adapter.PicAdapter.picid;
import static com.example.admin.yuejian21.Adapter.PicAdapter.picuserid;
import static com.example.admin.yuejian21.Adapter.PicAdapter.usernamest;
import static com.example.admin.yuejian21.activity.LoginActivity.photo;
import static com.example.admin.yuejian21.activity.LoginActivity.userid;
import static com.example.admin.yuejian21.activity.LoginActivity.username;

public class PicDetailActivity extends AppCompatActivity {
  private  RecyclerView recyclerView;
    private Pic pic;
    private ImageView userView;
    private ImageView picview;
    private TextView usernameView;
    private TextView titleview;
    String SessionID = "060pn7sjorpnq5fp53kid5st54";
    private Button commentButton;
    private EditText cpmment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_detail);
        userView = (ImageView) findViewById(R.id.imageView_stuser);
        picview =
                (ImageView) findViewById(R.id.imageView_stpic1);
        usernameView = (TextView) findViewById(R.id.textView_stusername);
        titleview = (TextView) findViewById(R.id.sttitle);
        commentButton= (Button) findViewById(R.id.button6_cButton);
        cpmment= (EditText) findViewById(R.id.editText3_ctext);

        pic = getIntent().getParcelableExtra("pic");

        if (pic != null) {
            if (!(null==usernamest)) {
                usernameView.setText(usernamest);
            } else {
                usernameView.setText(username);
            }
            Picasso
                    .with(PicDetailActivity.this)
                    .load("http://172.24.10.175/workout/Uploads/" + pic.getThumb0())


                    .into(picview);

            titleview.setText(pic.getTitle());
            if (!(photo1 == null))

            {
                Picasso
                        .with(PicDetailActivity.this)
                        .load("http://172.24.10.175/workout/Uploads/" + photo1)


                        .into(userView);
            }
        }
       recyclerView =
                (RecyclerView)findViewById(R.id.recyclerView);
        Retrofit retrofit=new Retrofit.Builder().baseUrl("http://172.24.10.175/workout/api.php/").addConverterFactory(GsonConverterFactory.create())
                .build();
        Service service=retrofit.create(Service.class);
        Call<List<comment>> call=service.reply(picid);
        call.enqueue(new Callback<List<comment>>() {
            @Override
            public void onResponse(Call<List<comment>> call, Response<List<comment>> response) {

                   List<comment> Commentlist = response.body();
                if(Commentlist!=null) {
                    CommentAdapter adapter = new CommentAdapter(PicDetailActivity.this, Commentlist);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PicDetailActivity.this);
                    linearLayoutManager.setOrientation(linearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                }

            }
            public void onFailure(Call<List<comment>> call, Throwable t) {

            }
        });
        commentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //拿到用户输入的用户名、密码.
                String comment = cpmment.getText().toString();


              Retrofit retrofit = new Retrofit.Builder()
                        .addConverterFactory(GsonConverterFactory.create())
                        .baseUrl("http://172.24.10.175/workout/api.php/")
                        .build();

                Service service = retrofit.create(Service.class);
               Call<Integer> call = service.comment(picuserid,picid,userid,comment);

                //3. 发送请求
                call.enqueue(new Callback<Integer>(){

                    @Override
                    public void onFailure(Call<Integer> arg0, Throwable arg1) {
                        Toast.makeText(PicDetailActivity.this, "发布失败", Toast.LENGTH_SHORT).show();

                    }
                    @Override
                    public void onResponse(Call<Integer> call, Response<Integer> response) {
                       if(response.body()!=0)


                       {
                           Toast.makeText(PicDetailActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                           ;
                       


                        }
                    }

                });
            }
        });
    }
}









